package com.liqi.web.controller.openapi.middleware;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.R;
import com.liqi.common.utils.Base64ToMultipartFile;
import com.liqi.common.utils.StringUtils;
import com.liqi.deployment.domain.DvDeployment;
import com.liqi.deployment.service.IDvDeploymentService;
import com.liqi.monitor.domain.dto.DvRemoteLogFlieDto;
import com.liqi.remote.domain.DvSaveLogFile;
import com.liqi.remote.service.IDvSaveLogFileService;
import com.liqi.web.controller.common.minio.MinIoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "提供给中间件的接口")
@RestController
@RequestMapping("/openApi/middleware")
public class DvSaveLogFileController extends BaseController{
    @Autowired
    private IDvDeploymentService dvDeploymentService;

    @Autowired
    private IDvSaveLogFileService dvSaveLogFileService;

    
	@Value("${ruoyi.profile}")
	private String profile;
	/**
	 * 拉取日志接口
	 */
	@ApiOperation("拉取日志接口")
	@PostMapping(value = "/saveLogFile")
	public R saveLogFile(@Validated @RequestBody DvRemoteLogFlieDto dvRemoteLogFlieDto) {
		DvSaveLogFile dvSaveLogFile = new DvSaveLogFile();

		String deviceId = dvRemoteLogFlieDto.getDeviceId();
		if (StringUtils.isBlank(deviceId)) {
			return R.fail("自编设备编号不能为空。" + deviceId);
		}		
		//业务逻辑
		DvDeployment dvDeployment = dvDeploymentService.getByDeviceId(deviceId);
		if (null == dvDeployment) {
			return R
					.fail("找不到该设备。"+ deviceId);     
		}
        try {
			String base64 =dvRemoteLogFlieDto.getLogFile();
			StringBuilder base = new StringBuilder(base64);
			base.insert(0,"data:application/zip;base64,");
			final String[] base64Array = base.toString().split(",");
			String dataUir = base64Array[0];
			String data = base64Array[1];

			MultipartFile multipartFile = new Base64ToMultipartFile(data, dataUir);
			String upload = this.fileUpload(multipartFile);
			dvSaveLogFile.setDeviceId(dvRemoteLogFlieDto.getDeviceId());
			dvSaveLogFile.setFileUrl(upload);
			dvSaveLogFile.setDateSpan(dvRemoteLogFlieDto.getLogDateSpan());
			dvSaveLogFile.setFileDate(dvRemoteLogFlieDto.getFileDate());
			dvSaveLogFile.setCreateBy("admin");
			dvSaveLogFile.setStatus(0);
			dvSaveLogFile.setMessageCode(dvRemoteLogFlieDto.getMessageCode());

		dvSaveLogFileService.insertDvRemoteLogFile(dvSaveLogFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
		return R.ok();

}
	
	
    public String fileUpload(MultipartFile file) {
        String path = profile;
        // 获取文件名(包括后缀)
        File folder = new File(path);
        // 判断路径是否存在,不存在则自动创建
        if(!folder.exists()){
            folder.mkdirs();
        }
        String fileName = file.getOriginalFilename();
        try{
        	
          file.transferTo(new File(folder,fileName));
          String filePath = path + File.separator + fileName;
           // fos.write(file.getBytes());
          return new String(filePath);
        } catch (Exception e) {
            return new String(e.getMessage());
        }
    }

	}